package course.service.impl;

import course.dao.StudentDao;
import course.dao.impl.StudentDaoImpl;
import course.pojo.entity.Student;
import course.pojo.vo.Page;
import course.service.StudentService;
import course.utils.JDBCUtil;

import java.sql.Connection;
import java.util.List;

/**
 * @author HLF
 * @date 2022/6/30 20:39
 */
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public Student getById(Integer sid) {
        Connection connection = JDBCUtil.getConnection();
        Student student = studentDao.getById(sid, connection);
        JDBCUtil.closeConnection(connection);
        return student;
    }

    @Override
    public int addStudent(Student student) {
        Connection connection = JDBCUtil.getConnection();
        int i = studentDao.addStudent(student, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int updateById(Student student) {
        Connection connection = JDBCUtil.getConnection();
        int i = studentDao.updateById(student, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int updateInfo(Student student) {
        Connection connection = JDBCUtil.getConnection();
        int i = studentDao.updateInfo(student, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public int deleteById(Integer sid) {
        Connection connection = JDBCUtil.getConnection();
        int i = studentDao.deleteById(sid, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public List<Student> getList() {
        Connection connection = JDBCUtil.getConnection();
        List<Student> list = studentDao.getList(connection);
        JDBCUtil.closeConnection(connection);
        return list;
    }

    /**/

    @Override
    public int deleteSelect(List<Integer> ids) {
        Connection connection = JDBCUtil.getConnection();
        int i = studentDao.deleteSelect(ids, connection);
        JDBCUtil.closeConnection(connection);
        return i;
    }

    @Override
    public long count() {
        Connection connection = JDBCUtil.getConnection();
        long count = studentDao.count(connection);
        JDBCUtil.closeConnection(connection);
        return count;
    }

    /**
     * 分页
     *
     * @param pageNo 当前页码数
     * @return 返回Page对象
     */
    @Override
    public Page<Student> page(Integer pageNo) {
        Connection connection = JDBCUtil.getConnection();
        Page<Student> page = studentDao.page(pageNo, connection);
        JDBCUtil.closeConnection(connection);
        return page;
    }

    /**
     * 模糊查询
     */
    @Override
    public Page<Student> query(String cname, Integer pageNo) {
        Connection connection = JDBCUtil.getConnection();
        Page<Student> page = studentDao.query(cname, pageNo, connection);
        JDBCUtil.closeConnection(connection);
        return page;
    }

    @Override
    public List<Student> selectBathByIds(List<Integer> ids) {
        Connection connection = JDBCUtil.getConnection();
        List<Student> students = studentDao.selectBathByIds(ids,connection);
        JDBCUtil.closeConnection(connection);
        return students;
    }
}
